package com.ruoyi.common.core.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.util.StringUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author guoweichao <guoweichao@kuaishou.com>
 * Created on 2020-11-23
 */
public class GsonUtil {

    private static final Gson GSON = new Gson();

    public static String toJSONString(Object object) {
        return object == null ? null : GSON.toJson(object);
    }

    public static <T> T parseObject(String text, Class<T> clazz) {
        return text == null ? null : GSON.fromJson(text, clazz);
    }

    /**
     * 将字符串形式对象 转成集合数组
     * @param text
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> parseArray(String text, Class<T> clazz) {
        if (StringUtils.isEmpty(text)) {
            text = "[]";
        }

        return (List) GSON.fromJson(text, TypeToken.getParameterized(ArrayList.class, new Type[] {clazz}).getType());
    }

    /**
     * 将json字符串转成json对象
     * @param text  字符串
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> parseMap(String text) {
        if (StringUtils.isEmpty(text)) {
            text = "{}";
        }
        return (Map<K, V>) GSON.fromJson(text, new TypeToken<Map<K,V>>(){}.getType());
    }

    /**
     * 通过Gson进行深度拷贝
     * @param object 需要拷贝的原始对象
     * @return 拷贝之后的对象
     */
    public static Object deepCloneObject(Object object) {
        if (object == null) {
            return null;
        }

        Class<?> aClass = object.getClass();
        System.out.println(aClass.getName());

        String json = GSON.toJson(object);
        return parseObject(json, aClass);
    }


    public static void main(String[] args) {
//        String s = "{\"templateCode\":\"MB2022061002\",\"jsonStr\":\"{\\\"originalCityA\\\":\\\"\\\",\\\"providentFundDate\\\":\\\"\\\",\\\"socialSecurityDate\\\":\\\"\\\",\\\"domicileA\\\":\\\"本埠城镇\\\",\\\"bParty\\\":{\\\"supplierName\\\":\\\"刘喆\\\",\\\"unifiedSocialCreditCode\\\":\\\"110108198504266813\\\"},\\\"domicile\\\":\\\"CiNpcy5laHIudG0ucGVyZm9ybWFuY2Utam9iTGV2ZWwudGVzdBIwWYUenKBe5Ocah8GwtWSYrJ0I6NddGTLR0dseYS6ycgmxlntFWg6SzOjJHBSA1q14GhJY26gNYqnBIH7ZGXn0a2VnSUYiIB30U7JgFmW4B27wWNEbT3wMH3N3wgHCZoH/tFVDWLzOMAE\\u003d\\\",\\\"originalCity\\\":\\\"\\\"}\"}\n";
       String s = "{\"valueAddedTaxaA\":null,\"exChannel\":\"Ncnc\",\"durationOfContract\":0,\"signingDateB\":\"2022-07-29\",\"thirdpaymenN\":\"Dhdn\",\"emailBoxC\":\"\",\"signingDateG\":\"2022-07-29\",\"bParty\":{\"Represent\":\"Nxnc\",\"unifiedSocialCreditCode\":\"Ncnfnffnn\",\"name\":\"Nxnc\",\"representMobile\":\"Ncn\",\"email\":\"123@gmail.com\",\"representAddress\":\"Jfjfjfn\"},\"companyNumber\":\"Ncnnc\",\"bankCodeA\":\"\",\"addressA\":\"xxx\",\"bankaCcountName\":\"\",\"registeredOffice\":\"Nc\",\"bankNameC\":\"\",\"bankAccountNum\":\"\",\"channelID\":\"psmvy646\",\"aParty\":{\"unifiedSocialCreditCode\":\"123\",\"name\":\"Wang Yong Qing\",\"authorizedRepresent\":\"Wang Yong Qing\",\"representMobile\":\"15049388730\",\"email\":\"wb_wangyongqing@kuaishou.com\",\"representAddress\":\"xxx\"},\"thirdMailboxA\":\"Qwe@gmail.com\",\"customeNumA\":\"Ncn\"}";
        Map<String, String> map = parseMap(s);
        System.out.println(map.keySet());
        System.out.println(map);
        System.out.println(GsonUtil.toJSONString(map));
    }

}
